using Blog2025.Application.Common;
using Blog2025.Application.Dto;
using Blog2025.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace Blog2025.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class UserController:ControllerBase
{
    // 用户管理控制器

    // 依赖注入

    private readonly IUserAppServices _service;
    public UserController(IUserAppServices service) => _service = service;

    [HttpGet]
    public async Task<ApiResult> GetAll([FromQuery]PageRequestDto pageRequestDto) => ApiResult.Success(await _service.GetAllAsync(pageRequestDto));

    [HttpGet("filter")]
    public async Task<IActionResult>  GetFilter()
    {
        var filters=new Dictionary<string,object>
        {
            {"username","丽丽"}
        };
        var res=await _service.GetFilterAsync(filters);
        return Ok(res);
    }

    [HttpGet("{id}")]
    public async Task<ApiResult> GetById(Guid id) => ApiResult.Success(await _service.GetByIdAsync(id));

    // 创建用户

    [HttpPost]
    public async Task<ApiResult> Create([FromBody] CreateUserDto dto)
    {
        await _service.CreateAsync(dto);
        return ApiResult.Success(message: "创建成功");
    }

    // 修改用户信息

    [HttpPut("{id}")]
    public async Task<ApiResult> Update(Guid id, [FromBody] UpdateUserDto dto)
    {
        await _service.UpdateAsync(id, dto);
        return ApiResult.Success(message: "更新成功");
    }
    
    // 删除用户

    [HttpDelete("{id}")]
    public async Task<ApiResult> Delete(Guid id)
    {
        await _service.DeleteAsync(id);
        return ApiResult.Success(message: "删除成功");
    }

    // 启用用户

    [HttpPost("{id}/enable")]
    public async Task<ApiResult> Enable(Guid id)
    {
        await _service.EnableAsync(id);
        return ApiResult.Success(message: "启用成功");
    }

    // 禁用用户

    [HttpPost("{id}/disable")]
    public async Task<ApiResult> Disable(Guid id)
    {
        var res = await _service.DisableAsync(id);
        return res;
    }

    // 分配角色

    [HttpPost("{id}/assign-role")]
    public async Task<ApiResult> AssignRole(Guid id, [FromBody] AssignRoleDto dto)
    {
        await _service.AssignRoleAsync(id, dto.RoleId);
        return ApiResult.Success(message: "分配角色成功");
    }

    // 移除角色

    [HttpPost("{id}/remove-role")]
    public async Task<ApiResult> RemoveRole(Guid id, [FromBody] AssignRoleDto dto)
    {
        await _service.RemoveRoleAsync(id, dto.RoleId);
        return ApiResult.Success(message: "移除角色成功");
    }

    // 头像更新

    [HttpPost("{id}/avatar")]
    public async Task<ApiResult> UpdateAvatar(Guid id, [FromBody] UpdateUserDto dto)
    {
        await _service.UpdateAvatarAsync(id, dto.AvatarUrl!);
        return ApiResult.Success(message: "头像更新成功");
    }

    // 昵称更新
    
    [HttpPost("{id}/nickname")]
    public async Task<ApiResult> UpdateNickname(Guid id, [FromBody] UpdateUserDto dto)
    {
        await _service.UpdateNicknameAsync(id, dto.Nickname!);
        return ApiResult.Success(message: "昵称更新成功");
    }

    // 密码更新

    [HttpPost("{id}/password")]
    public async Task<ApiResult> ChangePassword(Guid id, [FromBody] ChangePasswordDto dto)
    {
        await _service.ChangePasswordAsync(id, dto.OldPassword, dto.NewPassword);
        return ApiResult.Success(message: "密码修改成功");
    }
}